package com.xjtu.weixin.service.impl;

import com.xjtu.weixin.mapper.MpUserMapper;
import com.xjtu.weixin.model.entity.MpUser;
import com.xjtu.weixin.service.MpUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author 杨凯
 * @create 2018-08-22 9:53
 **/
@Service
@Slf4j
public class MpUserServiceImpl implements MpUserService {

	@Autowired
	private MpUserMapper userMapper;

	@Autowired
	private WxMpService wxMpService;

	@Override
	public int insertMpUser(WxMpUser user) {
		MpUser mpUser = userMapper.selectUserByOpenId(user.getOpenId());
		if (null == mpUser) {
			return userMapper.insertMpUser(user);
		} else {
			return userMapper.updateUserSubscribeStatus(true, user.getOpenId());
		}
	}

	@Override
	public MpUser selectUserByOpenId(String openId) {
		return userMapper.selectUserByOpenId(openId);
	}

	@Override
	public int updateUserSubscribeStatus(boolean status, String openId) {
		return userMapper.updateUserSubscribeStatus(status, openId);
	}

	@Override
	public List<MpUser> selectUserList() {
		return userMapper.selectUserList();
	}


	@Override
	public void updateUserRemark(String openId, String remark) throws WxErrorException {
		userMapper.updateUserRemark(openId,remark);
		wxMpService.getUserService().userUpdateRemark(openId,remark);
	}
}
